package cn.javase.base.io._01_File相关的流;

import org.junit.jupiter.api.Test;

import java.io.FileInputStream;
import java.io.IOException;

public class FileInputStreamTest {

    private static final String uri = "D:\\Code\\study\\JavaCode\\JavaSEDemo\\base\\src\\main\\java\\cn\\javase\\base\\io\\_01_File相关的流\\1.txt";

    /* 一个字节一个字节的读 */
    @Test
    public void testFileInputStream() throws IOException {
        FileInputStream fis = null;
        try {
            fis = new FileInputStream(uri);
            int readByte;
            while ((readByte = fis.read()) != -1) {
                System.out.print((char) readByte);
            }
        } catch (IOException e) {
            throw new RuntimeException(e);
        } finally {
            if (fis != null) {
                fis.close();
            }
        }
    }

    /* 多个字节的读 */
    @Test
    public void testFileInputStream2() throws IOException {
        FileInputStream fis = null;
        try {
            fis = new FileInputStream(uri);
            byte[] bytes = new byte[fis.available()];
            int readBytes;
            while ((readBytes = fis.read(bytes)) != -1) {
                String str = new String(bytes, 0, readBytes);
                System.out.print(str);
            }
        } catch (IOException e) {
            throw new RuntimeException(e);
        } finally {
            if (fis != null) {
                fis.close();
            }
        }
    }

}
